var usedSymptoms = [];

var firstsymsed="";
var tmpsyms="1";

//url中获取
var urlids = get("ids");
var urlsymptoms = get("symptoms");
if(urlsymptoms!=null){
	usedSymptoms.push(urlsymptoms);
}
var urlpart = get("part");
var symptomsArr = get("symptomsArr");

//当前id数组
var currentIds = [];

var newSymptoms = [];
var newIllName;

$(function() {
 	$("#used").hide();
 	$("#showTooltips2").hide();
 	getPartSymptomsByillList();
});

function get(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return  decodeURI(r[2]); return null;
}

//1
//根据上一步，获取对应部位的疾病病症
function getPartSymptomsByillList(){
	$.ajax({
        url: basePath+"illOper/getPartSymptomsByillList.spring",
        data: {
        	ids : urlids,
        	symptoms : urlsymptoms,
        	part : urlpart
        },
        type: "POST",
        dataType: 'json',
        cache: false,
        success: function (ret) {
            if(ret!=null){
            	currentIds = ret.ids;
            	spliceSymSelect(ret.symptoms);
            }
        }
    });
}

//2
//拼接一级病症
function spliceSymSelect(symptoms){
	$("#select1").html("");
	var html = '';
	//重新组拼页面
	html+='<option selected="" value="">请选择您感觉最不适合的症状</option>';
	html+='<option value="无">无</option>';
	for(var i=0;i<symptoms.length;i++){
		html+=" <option value="+symptoms[i]+">"+symptoms[i]+"</option>";
	}
	$("#select1").append(html);
}

//3
//通过onchange事件,当选择不为空和无时,选择二级病症
function selSecondSym(){
	
 	var syms=$('#select1 option:selected').val();//当前选择的病症
 	if(syms=='' || syms=='无'){
 		return;
 	}
	if(syms=="体温"){
		$("#value").attr("placeholder","°C");
	}else if(syms=="脉搏"){
		$("#value").attr("placeholder","次/分钟");
	}else if(syms=="收缩压" || syms=="舒张压"){
		$("#value").attr("placeholder","mmHg");
	}else{
		$("#value").attr("placeholder","");
	}
	if ($('#loadingToast').css('display') != 'none') {
		return;
	}
	console.log(currentIds);
	$('#loadingToast').show();
  	/*查询二级病症开始 */
	$.ajax({
	 	url:"./illOper/getSymptoms.spring",
	 	type: 'post',
	 	data: {
	          symptom: syms,
	          ids : currentIds.join(",")
		},
	 	dataType : 'json',
	 	async : false,
	 	traditional: true,
	 	success:function(ret){
	 		$("#used").show();
		 	$("#showtimes").show();
			$("#showdegree").show();
 			$("#shownature").show();
 			$("#showvalues").show();
 				
 			var degreeHTML;
 			var natureHTML;
 			var timeHTML;
 			if(ret.urls!=""){
 				popPicMenu();
 				var data=ret.urls;
 				var imgHTML="";
 				$("#IMGDIV").html("");
 				for(var i=0;i<data.length;i++){ //
 					imgHTML+="<i class=\"weui_icon_circle thei\" onclick=\"selPic();\"><img src=\"/img/"+data[i]+"\" width=\"65%\" id=\"img\" style=\"vertical-align: middle;margin: 8px;\"> </i><br>"
 				} 
 				$("#IMGDIV").append(imgHTML);
 			}
			if(ret.degrees!=null && ret.degrees!=""){
				$("#degree").html("");
				degreeHTML+='<option value="">'+''+'</option>';
				for(var i=0;i<ret.degrees.length;i++){
					degreeHTML+='<option value="'+ret.degrees[i]+'">'+ret.degrees[i]+'</option>';
				}
				$("#degree").append(degreeHTML);
	   			$('#loadingToast').hide();
			}else {
				$("#showdegree").hide();
	   			$('#loadingToast').hide();

			}
			
			if(ret.natures!=null && ret.natures!=""){
				$("#nature").html("");
				natureHTML+='<option value="">'+''+'</option>';
				for(var i=0,size1=ret.natures.length;i<size1;i++){
					natureHTML+='<option value="'+ret.natures[i]+'">'+ret.natures[i]+'</option>';
				}
				$("#nature").append(natureHTML);
	   			$('#loadingToast').hide();
			}else{
				$("#shownature").hide();
	   			$('#loadingToast').hide();
			}
			
			if(ret.timess!=null && ret.timess!=""){
				$("#times").html("");
				timeHTML+='<option value="">'+''+'</option>';
				for(var i=0,size1=ret.timess.length;i<size1;i++){
					timeHTML+='<option value="'+ret.timess[i]+'">'+ret.timess[i]+'</option>';
				}
				$("#times").append(timeHTML);
	   			$('#loadingToast').hide();
			}else{
				$("#showtimes").hide();
	   			$('#loadingToast').hide();
			}
		},
	});
	/*查询二级病症结束  */
}

//4
//点击'继续问诊',触发的事件，与部位相关
function clickYes(d){
	if(d==0){//结束问诊
		alert("您查询的疾病存在过多选项，未能得出具体疾病，请联系您主治医生，给您带来不便请谅解！");
		var backUrl=basePath+"inquiry/toSelfTest.spring";
		window.location.href=backUrl;
		$("#toast").hide();
	}else if(d==1){//该部位的继续问诊
		if ($('#loadingToast').css('display') != 'none') {
			return;
		}
		$('#loadingToast').show();
	
	 	var syms=$('#select1 option:selected').val();//当前选择的病症
	 	
		if(syms==""){
			alert("请选择项目!");
			$('#loadingToast').hide();
			return;
		}else if(syms=='无'){ //当选择无时，进行问这些疾病的非当前部位的病症
			$('#showTooltips1').hide();
			$('#showTooltips2').show();
			getSymsNopart();
		}else{
			getIllByIdAndsymptoms(syms);
		}
	}else if(d==2){//非该部位的继续问诊
		if ($('#loadingToast').css('display') != 'none') {
			return;
		}
		$('#loadingToast').show();
	
	 	var syms=$('#select1 option:selected').val();//当前选择的病症
	 	
		if(syms==""){
			alert("请选择项目!");
			$('#loadingToast').hide();
			return;
		}else if(syms=='无'){ //当选择无时，代表已经没有符合的病症，跳出此次查询
          	if(currentIds.length==0){//没有这种疾病
				alert("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
				var backUrl=basePath+"inquiry/toSelfTest.spring";
	            window.location.href=backUrl;
	        }else if(currentIds.length==1){//疾病只有一个，问剩下的病症
	        	
	        	$('#loadingToast').hide();
	        	getFreeSymptoms();
	        }else if(currentIds.length>1){//疾病有多个
	        	$('#loadingToast').hide();
				getAssistantExams(currentIds);
	        }else{//否则就提示没有疾病，并且返回上一页
   				$('#loadingToast').hide();
				alert("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
	            window.location.href=basePath+"inquiry/toSelfTest.spring";
	        }
		}else{
			getIllByIdAndsymptomsAndNoPart(syms);
		}
	}
}

//4.1
//获取当前疾病id列表的所有病症中的非当前部位的疾病
function getSymsNopart(){
	$.ajax({
		url:"./illOper/getNoPartSymptomsByillList.spring",
	 	type: 'post',
	 	data: {		  
       		ids : currentIds.join(","),
         	symptoms : usedSymptoms.join(","),
         	part :　urlpart
	   	},
	 	dataType : 'json',
	 	async : false,
	 	traditional: true,
	 	success:function(ret){
	 		//给全局ids赋值
        	currentIds = ret.ids;
        	//重新填充一级选择框
        	spliceSymSelect(ret.symptoms);
        	$('#loadingToast').hide();
	 	}
	});
}

//4.2
//根据当前ids列表和选取的病症，获取这些ids中该部位的病症
function getIllByIdAndsymptoms(syms){
	//用过的病症放到已用病症中
   	usedSymptoms.push(syms);
	   
   	var imgUrl=$("#imgRet").attr("src");//选择的图片病症
	var degree = $('#degree option:selected').val();//当前选择的病症
	var nature = $('#nature option:selected').val();//当前选择的病症
	var times = $('#times option:selected').val();//当前选择的病症
	var value = $('#value').val();//当前选择的病症
	
	//已用的病症拼接病历
	symptomsArr = spliceSymptom(syms,degree,nature,times,value,"");
	
	$.ajax({
		url:"./illOper/getIllByIdAndsymptoms.spring",
	 	type: 'post',
	 	data: {		  
       		ids : currentIds.join(","),
         	symptom : syms,
         	part : urlpart,
         	symptoms : usedSymptoms.join(","),
         	degree : degree,
         	nature : nature,
         	times :　times,
         	value : value,
         	imgurl:imgUrl
	   	},
	 	dataType : 'json',
	 	async : false,
	 	traditional: true,
	 	success:function(ret){
			$("#select1").empty();
		 	if(ret!=null){
		 		currentIds = ret.ids;
	          	if(currentIds.length==0){//没有这种疾病
					alert("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
					var backUrl=basePath+"inquiry/toSelfTest.spring";
		            window.location.href=backUrl;
		            $("#toast").hide();
		        }else if(currentIds.length==1){//疾病只有一个，问剩下的病症
		        	$('#loadingToast').hide();
		        	getIllByid(currentIds[0]);
		        	getFreeSymptoms();
		        }else if(currentIds.length>1&&ret.syms.length==0){//疾病有多个，病症已经没有了，去查找是否有辅助检查
		        	$('#loadingToast').hide();
					getAssistantExams(currentIds);
		        }else if(currentIds.length>1&&ret.syms.length>1){
		        	spliceSymSelect(ret.syms);
					$('#loadingToast').hide();
		        }else{//否则就提示没有疾病，并且返回上一页
	   				$('#loadingToast').hide();
					alert("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
		            window.location.href=basePath+"inquiry/toSelfTest.spring";
		        }
           	}
		},
	});
}

//4.3
//根据当前ids列表和选取的病症，获取这些ids中不是该部位的病症
function getIllByIdAndsymptomsAndNoPart(syms){
	//用过的病症放到已用病症中
 	usedSymptoms.push(syms);
	   
 	var imgUrl=$("#imgRet").attr("src");//选择的图片病症
	var degree = $('#degree option:selected').val();//当前选择的病症
	var nature = $('#nature option:selected').val();//当前选择的病症
	var times = $('#times option:selected').val();//当前选择的病症
	var value = $('#value').val();//当前选择的病症
	
	//已用的病症拼接病历
	symptomsArr = spliceSymptom(syms,degree,nature,times,value,"");
	
	$.ajax({
		url:"./illOper/getIllByIdAndsymptomsAndNoPart.spring",
	 	type: 'post',
	 	data: {		  
     		ids : currentIds.join(","),
	       	symptom : syms,
	       	part : urlpart,
	       	symptoms : usedSymptoms.join(","),
	       	degree : degree,
	       	nature : nature,
	       	times :　times,
	       	value : value,
	       	imgurl:imgUrl
	   	},
	 	dataType : 'json',
	 	async : false,
	 	traditional: true,
	 	success:function(ret){
			$("#select1").empty();
		 	if(ret!=null){
		 		currentIds = ret.ids;
	          	if(currentIds.length==0){//没有这种疾病
					alert("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
					var backUrl=basePath+"inquiry/toSelfTest.spring";
		            window.location.href=backUrl;
		            $("#toast").hide();
		        }else if(currentIds.length==1){//疾病只有一个，问剩下的病症
		        	$('#loadingToast').hide();
		        	getIllByid(currentIds[0]);
		        	getFreeSymptoms();
		        }else if(currentIds.length>1&&ret.syms.length==0){//疾病有多个，病症已经没有了，去查找是否有辅助检查
		        	$('#loadingToast').hide();
					getAssistantExams(currentIds);
		        }else if(currentIds.length>1&&ret.syms.length>1){
		        	spliceSymSelect(ret.symptoms);
					$('#loadingToast').hide();
		        }else{//否则就提示没有疾病，并且返回上一页
	   				$('#loadingToast').hide();
					alert("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
		            window.location.href=basePath+"inquiry/toSelfTest.spring";
		        }
         	}
		},
	});
}

//5
/**
 * 获取剩下的还未问的病症
 * */
function getFreeSymptoms(){
	$.ajax({
        url: "./illOper/getFreeSymptoms.spring",
        data: {
            ids : currentIds.join(","),
            symptoms:usedSymptoms.join(",")
        },
        type: "POST",
        dataType: 'json',
        cache: false,
        success: function (ret) {
            if(ret.symptoms.length>0){//还有病症
            	lastInquiry(ret.symptoms);
            }else{//没有病症，出结果
            	pop2();
            }
        }
    });
}

//6
//拼剩下的病症
function lastInquiry(symptoms){
	$("#times").val("");
	$("#degree").val("");
	$("#nature").val("");
	$("#values").val("");
	$("#showTooltips0").hide();
	$("#showTooltips1").hide();
	$("#showTooltips2").hide();
	$("#idyes").show();
	$("#idno").show();
/*	$("#asking2").show();
	$("#asking").hide();*/
	$("#used").show();
 	$("#showtimes").show();
	$("#showdegree").show();
	$("#shownature").show();
	$("#showvalues").hide();
	
	spliceSymSelect(symptoms);
}

//7
//点击是
function idyes(){
	if ($('#loadingToast').css('display') != 'none') {
		return;
	}
	$('#loadingToast').show();
	var id=$("#id").val();
	var syms=$('#select1 option:selected').val();//当前选择的病症
	var degree = $('#degree option:selected').val();//当前选择的病症
	var nature = $('#nature option:selected').val();//当前选择的病症
	var times = $('#times option:selected').val();//当前选择的病症
	var value = $('#value').val();//当前选择的病症
	if(syms==""){
		alert("请选择项目!");
		$('#loadingToast').hide();
		return;
	}else if(syms=='无'){
		pop2();
	}else{
		symptomsArr = spliceSymptom(syms,degree,nature,times,value,"");
		usedSymptoms.push(syms); 
		getFreeSymptoms();
	}

	$('#loadingToast').hide();
}
//点击否
function idno(){
	if ($('#loadingToast').css('display') != 'none') {
		return;
	}
	$('#loadingToast').show();
	var id=$("#id").val();
	var syms=$('#select1 option:selected').val();//当前选择的病症
	if(syms==""){
		alert("请选择项目!");
		$('#loadingToast').hide();
		return;
	}else if(syms=='无'){
		pop2();
	}else{
		usedSymptoms.push(syms);
		getFreeSymptoms();
	}

	$('#loadingToast').hide();
}


function nextorsubmit(){
	var question1=document.getElementById("#question1");
}

function spliceSymptom(symptom,degree,nature,times,value,danwei){
	if(symptom==null || symptom==""){
		return "";
	}
	symptomsArr += "病症："+symptom+"，";
	if(degree && degree!=""){
		symptomsArr += "程度："+degree+"，";
	}
	if(nature && nature!=""){
		symptomsArr += "性质："+nature+"，";
	}
	if(value && value!=""){
		symptomsArr += "数值："+value+danwei+"，";
	}
	var s = symptomsArr.substring(0,symptomsArr.length-1);
	return s +'；';
}

//获取辅助检查
function getAssistantExams(ids){
	
   	window.location.href = "./inquiry/toAssistantExams.spring?id="+ids;
}


function pop2(){
    var w_height = $(window).height();
    $('.historymenu').css('height',w_height);
    $('.historymenu').css('display','block');
    //   弹出边菜单
    $('.historymenu .bg_mark').css('display','block');
    
    $('.historymenu .historymenu_con ' ).stop().animate({ left: '0' }, 400);
    $('.cancle').click(function(){
    	  $('.historymenu').css('height',w_height);
        $('.historymenu .leftmenu_con ').stop().animate({ left: '-100%' }, 400);
        setTimeout(function(){
            $('.theleftmenu,.bg_mark').fadeOut('fast');
        },200);
    });
}
/**
 * 不遵守
 * @returns
 */
function noObserve(){
	var backurl=basePath+"inquiry/toSelfTest.spring";
	window.location.href = backurl;
}
/**
 * 遵守
 * @returns
 */
function observe(){
	var url1=basePath+"inquiry/toResult.spring";
	url1+="?illness="+newIllName;
	url1+="&symsused="+symptomsArr;
	window.location.href = url1;
}
/**
 * 弹出图片层
 * @returns
 */
function popPicMenu(){
    var w_height = $(window).height();
    $('.picmenu').css('height',w_height);
    $('.picmenu').css('display','block');
    //   弹出边菜单
    $('.picmenu .bg_mark').css('display','block');
    
    $('.picmenu .picmenu_con ' ).stop().animate({ left: '0' }, 400);
    $('.cancle').click(function(){
    	  $('.picmenu').css('height',w_height);
        $('.picmenu .picmenu_con ').stop().animate({ left: '-100%' }, 400);
        setTimeout(function(){
            $('.picmenu,.bg_mark').fadeOut('fast');
        },200);
    });
}

function queding(){
	 var w_height = $(window).height();
	  $('.picmenu').css('height',w_height);
    $('.picmenu .picmenu_con ').stop().animate({ left: '-100%' }, 400);
    setTimeout(function(){
        $('.picmenu,.bg_mark').fadeOut('fast');
    },200);
   
}
function selPic(){
	//单选
	$("#IMGDIV i").click(function(){
		var cls=$(this).removeClass("weui_icon_circle");
		cls.addClass("weui_icon_success active").siblings('i').removeClass("weui_icon_success").addClass("weui_icon_circle");
		$("#imgRet").attr("src",$(this).children().attr("src"));
		$("#imgRet").show();
	});	
}
//当id只有一个的时候，按id获取疾病
function getIllByid(id){
	$.ajax({
        url: "./illness/getIllById.spring",
        data: {
            id : id
        },
        type: "POST",
        dataType: 'json',
        cache: false,
        success: function (ret) {
            if(ret!=null){
            	newIllName = ret.illness;
            	console.log("newIllName====="+newIllName);
            }else{
            	$.messager.popup("您查询的疾病不存在，请联系您主治医生，给您带来不便请谅解！");
            }
        }
    });
}